package com.example.park.controller;

import com.example.common.utils.R;
import com.example.park.entity.ParkShareEntity;
import com.example.park.entity.ParkingEntity;
import com.example.park.entity.UserEntity;
import com.example.park.entityVO.*;
import com.example.park.service.ParkShareService;
import com.example.park.service.ParkingService;
import com.example.park.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.jms.Destination;
import javax.jms.Queue;
import javax.jms.Topic;
import java.util.List;

import static com.example.park.mqtt.TopicConsumerListener.is_success;

@Api(tags = "普通用户接口")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private ParkingService parkingService;

    @Autowired
    private ParkShareService parkShareService;

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    private Queue queue;

    @Autowired
    private Topic topic;

    /**
     * 完善个人信息
     * @param user
     * @return
     */
    @ApiOperation("完善个人信息")
    @RequestMapping(value = "/updateUser", method = RequestMethod.POST)
    public R updateUser(@RequestBody UserEntity user) {
        if (userService.update(user)) {
            return R.ok("更新成功！");
        }
        return R.error("更新失败！");
    }

    /*****************************  车位主接口  *****************************/

    /**
     * 查找相关车位
     * @param vo , uId为1时查找未绑定, 为2时查找已绑定
     * @return
     */
    @ApiOperation("查找相关车位")
    @RequestMapping(value = "/queryParkingByParam", method = RequestMethod.POST)
    public R queryParkingByParam(@RequestBody ParkingDimVO vo) {
        List<ParkingVO> vos = parkingService.queryVOByDim(vo);
        return R.ok().put("list", vos);
    }

    /**
     * 查找未绑定车位
     * @param weiXinParking
     * @return
     */
    @ApiOperation("查找未绑定车位")
    @RequestMapping(value = "/weiXinQuery", method = RequestMethod.POST)
    public R weiXinQuery(@RequestBody WeiXinParking weiXinParking) {
        List<ParkingEntity> vos = parkingService.weiXinQuery(weiXinParking);
        return R.ok().put("list", vos);
    }

    @ApiOperation("查找车位信息")
    @RequestMapping(value = "/queryParkingById", method = RequestMethod.POST)
    public R queryParkingById(@RequestBody ParkingVO vo) {
        List<ParkingVO> vos = parkingService.queryVOByParam(vo);
        return R.ok().put("list", vos);
    }

    @ApiOperation("根据uId查找车位信息")
    @RequestMapping(value = "/queryParkingByUId", method = RequestMethod.POST)
    public R queryParkingByUId(@RequestBody ParkingVO vo) {
        List<ParkingVO> vos = parkingService.queryVOByParam(vo);
        return R.ok().put("list", vos);
    }

    /**
     * 绑定车位
     * @param pe
     * @return
     */
    @ApiOperation("绑定车位")
    @RequestMapping(value = "/bindParking", method = RequestMethod.POST)
    public R bindParking(@RequestBody ParkingVO pe) {
        String result = parkingService.bindParking(pe);
        return R.ok(result);
    }

    /**
     * 查看发布车位共享时间
     * @param pId
     * @return
     */
    @ApiOperation("查看发布车位共享时间")
    @RequestMapping(value = "/pushTimeList", method = RequestMethod.POST)
    public R pushTimeList(Integer pId) {
        ParkShareVO vo = new ParkShareVO();
        vo.setpId(pId);
        List<ParkShareVO> vos = parkShareService.queryByParam(vo);
        return R.ok().put("list", vos);
    }

    /**
     * 发布车位共享时间
     * @param ps
     * @return
     */
    @ApiOperation("发布车位共享时间")
    @RequestMapping(value = "/pushTime", method = RequestMethod.POST)
    public R pushTime(@Validated @RequestBody ParkShareEntity ps) {
        String result = parkShareService.pushTime(ps);
        return R.ok(result);
    }

    /**
     * 取消共享时间
     * @param id
     * @return
     */
    @ApiOperation("取消共享时间")
    @RequestMapping(value = "/deleteTime", method = RequestMethod.POST)
    public R deleteTime(Integer id) {
        if (parkShareService.delete(id)) {
            return R.ok("删除成功！");
        }
        return R.error("删除失败！");
    }

    /*****************************  租赁接口  *****************************/

    /**
     * 查看当前闲置车位
     * @param vo
     * @return
     */
    @ApiOperation("查看当前闲置车位")
    @RequestMapping(value = "/queryParking", method = RequestMethod.POST)
    public R queryParking(@RequestBody ParkShareDimVO vo) {
        List<ParkShareVO> vos = parkShareService.queryByDim(vo);
        return R.ok().put("list", vos);
    }

    /**
     * 根据id查找闲置车位
     * @param id
     * @return
     */
    @ApiOperation("根据id查找闲置车位")
    @RequestMapping(value = "/queryShareById", method = RequestMethod.POST)
    public R queryShareById(Integer id) {
        List<ParkShareVO> vos = parkShareService.queryById(id);
        return R.ok().put("list", vos);
    }

    /**
     * 租赁
     * @param ps
     * @return
     */
    @ApiOperation("租赁")
    @RequestMapping(value = "/rent", method = RequestMethod.POST)
    public R rent(@RequestBody ParkShareEntity ps) {
        ps.setState(1);
        if (parkShareService.update(ps)) {
            return R.ok("租赁成功！");
        }
        return R.error("租赁失败！");
    }

    /**
     * 查看历史租赁车位
     * @param uId
     * @return
     */
    @ApiOperation("查看历史租赁车位")
    @RequestMapping(value = "historyRent", method = RequestMethod.POST)
    public R historyRent(Integer uId) {
        List<ParkShareVO> vos = parkShareService.queryByUid(uId);
        return R.ok().put("list", vos);
    }

    @RequestMapping(value = "/queue/test", method = RequestMethod.POST)
    public String sendQueue(@RequestBody String str) {
        this.sendMessage(this.queue, str);
        return "success";
    }

    @RequestMapping(value = "/topic/test", method = RequestMethod.POST)
    public String sendTopic(@RequestBody String str) {
        this.sendMessage(this.topic, str);
        return "success";
    }

    @RequestMapping(value = "/success", method = RequestMethod.POST)
    public String success() {
        return is_success;
    }

    // 发送消息，destination是发送到的队列，message是待发送的消息
    private void sendMessage(Destination destination, final String message){
        jmsMessagingTemplate.convertAndSend(destination, message);
    }
}
